﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using DataObjects.EFData;

namespace DataObjects
{
    public class HitCounter : IHitCounter
    {
        private WebOnlineEntities datacontext;
        public long GetHitCounter()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var hitcounter = datacontext.TB_HitCounter.FirstOrDefault();
                if (hitcounter != null)
                {
                    return hitcounter.HitCounter;
                }
                return 0;
            }
        }

        public bool Counting()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var hitcounter = datacontext.TB_HitCounter.FirstOrDefault();
                if (hitcounter != null)
                {
                    hitcounter.HitCounter++;
                }
                else
                {
                    datacontext.AddToTB_HitCounter(new TB_HitCounter() { HitCounter = 1 });
                }
                return datacontext.SaveChanges() > 0;
            }
        }
    }
}
